<title>管理员</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>用户</cite></a>
        <a><cite>管理员</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">管理员列表</div>
                <div class="layui-card-body">
                    <div class="layui-upload">
                        <button id="addAdmin" type="button" class="layui-btn layui-btn-normal"><i class="layui-icon layui-icon-add-1"></i>添加</button>
                        <div class="layui-upload-list">
                            <table class="layui-table" frame="void" id="adminTable">
                                <thead>
                                <tr>
                                    <th>学号</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody id="adminTbody"></tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="lineBar">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
</script>

<script>
    layui.use(['jquery', 'table', 'admin'], function(){
        var $ = layui.$, form = layui.form,
            table = layui.table, admin = layui.admin, view = layui.view;

        let adminConfigUrl = "/admin/manager";

        function renderTable() {
            $.ajax({
                url:        adminConfigUrl,
                dataType:   'JSON',
                complete:   function (result) {
                    result = result.responseJSON;
                    let status = result['status'];
                    if (status !== 200) {
                        layer.msg(result['message']);
                    } else {
                        renderAdminList(result['data'])
                    }
                }
            })
        }

        function renderAdminList(adminSet) {
            $('#adminTbody').empty();
            adminSet.forEach(function (userCode) {
                let tr = $([
                    "<tr>"
                    ,"<td>" + userCode + "</td>"
                    ,"<td><a name='del-" + userCode + "' class=\"layui-btn layui-btn-danger layui-btn-xs\"><i class=\"layui-icon layui-icon-delete\"></i>删除</a></td>"
                    ,"</tr>"
                ].join(''));
                tr.find('.layui-btn-danger').on('click', function () {
                   let userCode = this.name.substring(4);
                   delAdmin(userCode);
                });
                $('#adminTbody').append(tr);
            })
        }

        /**
         * 监听添加管理员按钮
         */
        $('#addAdmin').click(function (event) {
            layer.open({
                title: '输入学号'
                ,content: '<input type="text" id="userCode" name="userCode" class="layui-input">'
                ,yes: function(index, layero){
                    let userCode = $('#userCode').val();
                    addAdmin(userCode);
                    layer.close(index);
                }
            });
        });

        /**
         * 添加管理员
         */
        function addAdmin(userCode) {
            let load = layer.load(2);
            let data = {
                'userCode': userCode
            };
            $.ajax({
                url:        adminConfigUrl,
                type:       'POST',
                dataType:   'JSON',
                data:       data,
                complete:   function (result) {
                    layer.close(load);
                    result = result.responseJSON;
                    let status = result['status'];
                    if (status !== 200) {
                        layer.msg(result['message']);
                    } else {
                        layer.msg('添加成功');
                        renderTable();
                    }
                }
            })
        }

        /**
         * 删除管理员
         * @param userCode
         */
        function delAdmin(userCode) {
            layer.open({
                title: '删除管理员'
                ,content: '确认删除 ' + userCode
                ,yes: function(index, layero){
                    let load = layer.load(2);
                    let data = {
                        'userCode': userCode
                    };
                    $.ajax({
                        url:        adminConfigUrl,
                        type:       'DELETE',
                        dataType:   'JSON',
                        data:       data,
                        complete:   function (result) {
                            layer.close(load);
                            result = result.responseJSON;
                            let status = result['status'];
                            if (status !== 200) {
                                layer.msg(result['message']);
                            } else {
                                layer.msg('删除成功');
                                renderTable();
                            }
                        }
                    })
                }
            });
        }

        renderTable();
    });
</script>